﻿<Window x:Class="AGMLite.Windows.ItemEditWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:AGMLite.Windows"
        Title="HP Agile Management Lite - Item Edit" MinHeight="600" MinWidth="800" SizeToContent="WidthAndHeight" Icon="/AGMLite;component/images/agm.ico">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style TargetType="{x:Type Image}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type UIElement}, AncestorLevel=1}, Path=IsEnabled}"  Value="False">
                                <Setter Property="Opacity"  Value="0.5" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                    <Style TargetType="Control" x:Key="myErrorTemplate">
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="true">
                                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                    <Style TargetType="TextBox" BasedOn="{StaticResource myErrorTemplate}" />
                    <Style TargetType="CheckBox" BasedOn="{StaticResource myErrorTemplate}" />
                    <Style TargetType="ComboBox" BasedOn="{StaticResource myErrorTemplate}" />
                    <DataTemplate x:Key="ComboBoxTemplate">
                        <ComboBox ItemsSource="{Binding AvailableFieldOptions, Mode=TwoWay}" SelectedValuePath="Key" DisplayMemberPath="Value" 
                                  SelectedValue="{Binding Field.Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                  MinWidth="300" />
                    </DataTemplate>
                    <DataTemplate x:Key="TextBlockTemplate">
                        <TextBlock Text="{Binding Field.Value, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" MinWidth="300" TextWrapping="Wrap"></TextBlock>
                    </DataTemplate>
                    <DataTemplate x:Key="TextBoxTemplate">
                        <TextBox Text="{Binding Field.Value, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" MinWidth="300"></TextBox>
                    </DataTemplate>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                <StackPanel Orientation="Horizontal" Margin="4">
                    <TextBlock Text="Is Enabled:" Width="100" VerticalAlignment="Center"></TextBlock>
                    <CheckBox VerticalAlignment="Center" Name="cbIsEnabled" IsChecked="{Binding Item.IsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="4">
                <TextBlock Text="Entity Type:" Width="100" VerticalAlignment="Center"></TextBlock>
                <ComboBox Name="cmbEntityType" Width="250" 
                          IsEnabled="{Binding Item.CanEditEntityType, Mode=TwoWay}"
                          SelectedItem="{Binding Item.EntityType, UpdateSourceTrigger=LostFocus, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                          SelectionChanged="cmbEntityType_SelectionChanged">
                </ComboBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="4">
                <TextBlock Text="Item Name:" Width="100" VerticalAlignment="Center"></TextBlock>
                <TextBox Name="txtName" Width="250" Text="{Binding Item.Name, UpdateSourceTrigger=LostFocus, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
            </StackPanel>            
            <StackPanel Orientation="Horizontal" Margin="4">
                <TextBlock Text="Sync Interval:" Width="100" VerticalAlignment="Center"></TextBlock>
                <TextBox Name="txtSyncInterval" Width="250" Text="{Binding Item.SyncInterval, UpdateSourceTrigger=LostFocus, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="4">                                    
                        <StackPanel Orientation="Vertical">
                        <ToolBar>
                            <Button Name="btnAddFilterField" Command="{Binding AddFilterFieldCommand}">
                                <Image Source="/AGMLite;component/images/add.png" Height="20" Width="20" />
                            </Button>
                            <Button Name="btnDeleteFilterField" Command="{Binding DeleteFilterFieldCommand}"  CommandParameter="{Binding ElementName=dataGridFilterField, Path=SelectedItem}">
                                <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" />
                            </Button>
                        </ToolBar>
                        <GroupBox Header="I want to monitor items with following filter conditions" Margin="2">
                            <DataGrid AutoGenerateColumns="False" 
                                      HorizontalAlignment="Stretch" 
                                      Name="dataGridFilterField" 
                                      VerticalAlignment="Stretch" 
                                      SelectionMode="Single" 
                                      ItemsSource="{Binding Item.FilterFieldCollection}"
                                      RowHeaderWidth="0" 
                                      CanUserSortColumns="False" 
                                      CanUserReorderColumns="False" 
                                      CanUserAddRows="False" 
                                      CanUserDeleteRows="False"
                                      EnableRowVirtualization="True"
                                      ScrollViewer.IsDeferredScrollingEnabled="True"
                                      MaxHeight="300">
                                    <DataGrid.InputBindings>
                                        <KeyBinding Key="DELETE" Command="{Binding DeleteFilterFieldCommand}" CommandParameter="{Binding SelectedItems, ElementName=dataGridFilterField}" />
                                    </DataGrid.InputBindings>
                                    <DataGrid.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="Delete" Command="{Binding DeleteFilterFieldCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}">
                                                <MenuItem.Icon>
                                                    <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" />
                                                </MenuItem.Icon>
                                            </MenuItem>
                                        </ContextMenu>
                                    </DataGrid.ContextMenu>
                                    <DataGrid.Columns>
                                    <DataGridComboBoxColumn Header="Type" Width="150">
                                            <DataGridComboBoxColumn.ElementStyle>
                                                <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFieldTypes, Mode=TwoWay}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Field.Entity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                            </DataGridComboBoxColumn.ElementStyle>
                                            <DataGridComboBoxColumn.EditingElementStyle>
                                                <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFieldTypes, Mode=TwoWay}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Field.Entity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                            </DataGridComboBoxColumn.EditingElementStyle>
                                        </DataGridComboBoxColumn>
                                    <DataGridComboBoxColumn Header="Field" Width="150" >
                                        <DataGridComboBoxColumn.ElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFields, Mode=TwoWay}"/>
                                                <Setter Property="DisplayMemberPath" Value="Label"/>
                                                <Setter Property="SelectedValuePath" Value="Name"/>
                                                <Setter Property="SelectedValue" Value="{Binding Field.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.ElementStyle>
                                        <DataGridComboBoxColumn.EditingElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFields, Mode=TwoWay}"/>
                                                <Setter Property="DisplayMemberPath" Value="Label"/>
                                                <Setter Property="SelectedValuePath" Value="Name"/>
                                                <Setter Property="SelectedValue" Value="{Binding Field.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.EditingElementStyle>
                                    </DataGridComboBoxColumn>
                                        <DataGridComboBoxColumn x:Name="cmbOperation" Header="Operation" Width="120" >
                                            <DataGridComboBoxColumn.ElementStyle>
                                                <Style TargetType="ComboBox">
                                                    <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterFieldOperations}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Operation, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                            </DataGridComboBoxColumn.ElementStyle>
                                            <DataGridComboBoxColumn.EditingElementStyle>
                                                <Style TargetType="ComboBox">
                                                    <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterFieldOperations}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Operation, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                            </DataGridComboBoxColumn.EditingElementStyle>
                                        </DataGridComboBoxColumn>
                                    <DataGridTemplateColumn Header="Value">
                                            <DataGridTemplateColumn.CellTemplateSelector>
                                        <local:FieldValueTemplateSelector ComboBoxTemplate="{StaticResource TextBlockTemplate}" TextBoxTemplate="{StaticResource TextBlockTemplate}"/>
                                            </DataGridTemplateColumn.CellTemplateSelector>
                                        <DataGridTemplateColumn.CellEditingTemplateSelector>
                                            <local:FieldValueTemplateSelector ComboBoxTemplate="{StaticResource ComboBoxTemplate}" TextBoxTemplate="{StaticResource TextBoxTemplate}"/>
                                        </DataGridTemplateColumn.CellEditingTemplateSelector>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                        </GroupBox>
                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <ToolBar>
                            <Button Name="btnAddViewField" Command="{Binding AddViewFieldCommand}">
                                <Image Source="/AGMLite;component/images/add.png" Height="20" Width="20" />
                            </Button>
                            <Button Name="btnDeleteViewField" Command="{Binding DeleteViewFieldCommand}"  CommandParameter="{Binding ElementName=dataGridViewField, Path=SelectedItem}">
                                <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" />
                            </Button>
                            <Button Name="btnMoveUpViewField" Command="{Binding MoveUpViewFieldCommand}"  CommandParameter="{Binding ElementName=dataGridViewField, Path=SelectedItem}">
                                <Image Source="/AGMLite;component/images/up.png" Height="20" Width="20"  ToolTip="Move Up" />
                            </Button>
                            <Button Name="btnMoveDownViewField" Command="{Binding MoveDownViewFieldCommand}"  CommandParameter="{Binding ElementName=dataGridViewField, Path=SelectedItem}">
                                <Image Source="/AGMLite;component/images/down.png" Height="20" Width="20" ToolTip="Move Down" />
                            </Button>
                        </ToolBar>
                        <GroupBox Header="I want to view the following fields for the entity of this item" Margin="2">
                            <DataGrid AutoGenerateColumns="False" 
                                      HorizontalAlignment="Stretch" 
                                      Name="dataGridViewField" 
                                      VerticalAlignment="Stretch" 
                                      SelectionMode="Single" 
                                      ItemsSource="{Binding Item.ViewFieldCollection}"
                                      CanUserSortColumns="False" 
                                      CanUserReorderColumns="False" 
                                      CanUserAddRows="False" 
                                      CanUserDeleteRows="False"
                                      EnableRowVirtualization="True"
                                      ScrollViewer.IsDeferredScrollingEnabled="True"
                                      MaxHeight="300">
                                    <DataGrid.InputBindings>
                                        <KeyBinding Key="DELETE" Command="{Binding DeleteViewFieldCommand}" CommandParameter="{Binding SelectedItems, ElementName=dataGridViewField}" />
                                    </DataGrid.InputBindings>
                                    <DataGrid.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="Delete" Command="{Binding DeleteViewFieldCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}">
                                                <MenuItem.Icon>
                                                    <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" />
                                                </MenuItem.Icon>
                                            </MenuItem>
                                        <MenuItem Header="Move Up" Command="{Binding MoveUpViewFieldCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}">
                                            <MenuItem.Icon>
                                                <Image Source="/AGMLite;component/images/up.png" Height="20" Width="20" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="Move Down" Command="{Binding MoveDownViewFieldCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}">
                                            <MenuItem.Icon>
                                                <Image Source="/AGMLite;component/images/down.png" Height="20" Width="20" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                    </DataGrid.ContextMenu>
                                    <DataGrid.Columns>
                                        <DataGridComboBoxColumn Header="Type" Width="150">
                                        <DataGridComboBoxColumn.ElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFieldTypes, Mode=TwoWay}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Field.Entity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.ElementStyle>
                                        <DataGridComboBoxColumn.EditingElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFieldTypes, Mode=TwoWay}"/>
                                                <Setter Property="SelectedItem" Value="{Binding Field.Entity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.EditingElementStyle>
                                    </DataGridComboBoxColumn>
                                    <DataGridComboBoxColumn Header="Field" Width="150" >
                                        <DataGridComboBoxColumn.ElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFields, Mode=TwoWay}"/>
                                                <Setter Property="DisplayMemberPath" Value="Label"/>
                                                <Setter Property="SelectedValuePath" Value="Name"/>
                                                <Setter Property="SelectedValue" Value="{Binding Field.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.ElementStyle>
                                        <DataGridComboBoxColumn.EditingElementStyle>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="ItemsSource" Value="{Binding AvailableFields, Mode=TwoWay}"/>
                                                <Setter Property="DisplayMemberPath" Value="Label"/>
                                                <Setter Property="SelectedValuePath" Value="Name"/>
                                                <Setter Property="SelectedValue" Value="{Binding Field.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                            </Style>
                                        </DataGridComboBoxColumn.EditingElementStyle>
                                    </DataGridComboBoxColumn>
                                </DataGrid.Columns>
                                </DataGrid>
                        </GroupBox>
                    </StackPanel>
                </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="4" HorizontalAlignment="Center">
                <Button Name="btnSave" Width="70" Margin="2" Content="Save" Command="{Binding SaveCommand}"></Button>
                <Button Name="btnCancel" Width="70" Margin="2" Content="Cancel" Command="{Binding CancelCommand}"></Button>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>
